import MonoSingle from "../Utility/MonoSingle";
import BaseInputEvent from "./BaseInputEvent";
import TvBoxInput from "./TvBoxInput";
import SetAnchorEvent from "../Utility/SetAnchorEvent";

const { ccclass } = cc._decorator;
@ccclass
export default class TvBoxButtonListEvent extends cc.Component {
    private btnListEvent: Array<cc.Button> = new Array<cc.Button>();
    private selectCilckMap = new Map();

    clearFingerEvent() {
        this.btnListEvent.forEach(element => {
            if (element.node) {
                let img_select_click = element.node.getChildByName("img_select_cilck")
                if (img_select_click)
                    img_select_click.active = false;
            }
        });
    }
    ResetBtnListEvent(): void {
        this.btnListEvent.length = 0;
        MonoSingle.GetTvBoxInput().curBtn = null;
        MonoSingle.GetTvBoxInput().btnListHorRight.length = 0;
        MonoSingle.GetTvBoxInput().btnListHorLeft.length = 0;
        MonoSingle.GetTvBoxInput().btnListVerUp.length = 0;
        MonoSingle.GetTvBoxInput().btnListVerDown.length = 0;
    }
    public AddValueToBtnListEvent(btn: cc.Button, imgPath: string = "img_select_cilck_finger", _scaleX: number = 1, _scaleY: number = 1): void {
        if (this.selectCilckMap.size == 0) {
            let self = this;
            cc.loader.loadRes("tvboxPrefabs/img_select_cilck_finger", cc.Prefab, function name(error, prefab) {
                self.selectCilckMap.set("img_select_cilck_finger", prefab);
                self.setSelectClickSpriteState(btn, imgPath, _scaleX, _scaleY);
            });
        }
        else {
            this.setSelectClickSpriteState(btn, imgPath, _scaleX, _scaleY);
        }
    }

    private setSelectClickSpriteState(btn: cc.Button, imgPath: string, _scaleX: number, _scaleY: number) {
        let img_select_click = btn.node.getChildByName("img_select_cilck")
		if (img_select_click == null) {
            let img_select_click_go = cc.instantiate(this.selectCilckMap.get(imgPath));
			if (img_select_click_go==null)
				return
            img_select_click_go.setParent(btn.node);
            img_select_click_go.name = "img_select_cilck";
            img_select_click_go.active = false;
			// img_select_click_go.setScale(1 / btn.node.scaleX, 1 / btn.node.scaleY);
			img_select_click_go.setScale(0.5, 0.5);
            img_select_click_go.setAnchorPoint(-0.2, 1);
            if (btn.node.getComponent(SetAnchorEvent)) {
                img_select_click_go.setAnchorPoint(btn.node.getComponent(SetAnchorEvent).anchorX, btn.node.getComponent(SetAnchorEvent).anchorY);
                img_select_click_go.setScale(btn.node.getComponent(SetAnchorEvent).scale, btn.node.getComponent(SetAnchorEvent).scale);
            }
            img_select_click_go.setPosition(0, 0);
			// img_select_click_go.angle = 0;
			img_select_click_go.angle = -btn.node.angle;
            let _offValue = Math.min(btn.node.getContentSize().width, btn.node.getContentSize().height);
            _offValue = Math.min(btn.node.getContentSize().width, 100);
            img_select_click_go.setContentSize(_offValue, _offValue);

        }
        this.btnListEvent.push(btn)
        if (MonoSingle.GetBaseInputEvent().btns.length == this.btnListEvent.length) {
            MonoSingle.GetTvBoxInput().arraySortEvent();
            this.SetBtnListEventUISelectState();
        }
    }

    public SetBtnListEventUISelectState(): void {
        if (this.btnListEvent.length == 0)
            return;
        this.btnListEvent.forEach(element => {
            let img_select_click = element.node.getChildByName("img_select_cilck")
            if (img_select_click)
                img_select_click.active = false;
            if (MonoSingle.GetTvBoxInput().curBtn == element)
                img_select_click.active = true;
        });
    }
    public SetSelectedGameObjectEvent(isReset: boolean = true): void {
        if (MonoSingle.GetBaseInputEvent().btns.length == 0)
            return;
        if (isReset) {
            MonoSingle.GetTvBoxInput().curBtn = MonoSingle.GetBaseInputEvent().btns[0];
        }
    }
}

